package com.shujia.flink.tf

import org.apache.flink.api.java.functions.KeySelector
import org.apache.flink.streaming.api.scala._

object Demo1KeyBy {
  def main(args: Array[String]): Unit = {

    //创建flink的运行环境
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    env.setParallelism(2)


    val ds: DataStream[String] = env.socketTextStream("master", 8888)


    /**
      * keyBy 将同一个单词分到同一个流中
      *
      */
    val keyByDS: KeyedStream[String, String] = ds.keyBy(w => w)

    keyByDS.print()


    val countDS: DataStream[(String, Int)] = ds
      .map((_, 1))
      .keyBy(_._1)
      //由于前面做了keyby ，所以reduce是对同一个单词的聚合
      .reduce((kv1, kv2) => (kv1._1, kv1._2 + kv2._2)) //多value景区聚合


    countDS.print()


    env.execute()


  }
}
